<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_News_Services_Cron
{
	public $addArticles = array();
	public $addCategories = array();
	
	public function __construct()
	{
		ini_set('memory_limit', '256M');
		require_once LIB_DIR . DS . 'simplehtmldom' . DS . 'simple_html_dom.php';		
	}
	
	public static function validate($data)
	{
		$validator = new Gio_Core_Validator();
		foreach ($data as $index => $value) {
			if (gettype($value) == 'string') {
				$data[$index] = trim($value);
			}
		} 
		
		$data['messages']['name'] = $validator->setString($data['name'])
												->validate(array('notNull')
																, array(
																	'notNull' => 'cron_name_notnull',
																)
														);
		
		$data['messages']['website_name'] = $validator->setString($data['website_name'])
												->validate(array('notNull', 'isIdString')
																, array(
																	'notNull' => 'cron_website_name_notnull',
																	'isIdString' => 'cron_website_name_invalid',
																)
														);
		
		$data['messages']['website'] = $validator->setString($data['website'])
												->validate(array('notNull')
																, array(
																	'notNull' => 'cron_website_notnull',
																)
														);
		
		$data['messages']['source'] = $validator->setString($data['source'])
												->validate(array('notNull')
																, array(
																	'notNull' => 'cron_source_notnull',
																)
														);
														
		$data['messages']['main_category_id'] = $validator->setString($data['main_category_id'])
												->validate(array('notNull')
																, array(
																	'notNull' => 'cron_main_category_id_notnull',
																)
														);
														
		$data['messages']['frequency'] = $validator->setString($data['frequency'])
												->validate(array('isNumeric')
																, array(
																	'isNumeric' => 'cron_frequency_invalid',
																)
														);
		
		$error = false;
		foreach ($data['messages'] as $index => $message) {
			if ($message) {
				$data['messages_error'] = true;
				return $data;
			}
		}										
		$data['messages_error'] = false;
		return $data; 
	}
	
	public static function add($cron)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_News_Models_Mysql_Cron();
		$dao->setConnection($conn);
		return $dao->add($cron);
	}
	
	public static function delete($cronId)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_News_Models_Mysql_Cron();
		$dao->setConnection($conn);
		return $dao->delete($cronId);
	}
	
	public static function update($cron)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_News_Models_Mysql_Cron();
		$dao->setConnection($conn);
		return $dao->update($cron);
	}
	
	public static function getById($id)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_News_Models_Mysql_Cron();
		$dao->setConnection($conn);
		return $dao->getById($id);
	}
	
	public static function find($offset = null, $limit = null, $condition = array())
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_News_Models_Mysql_Cron();
		$dao->setConnection($conn);
		return $dao->find($offset, $limit, $condition);
	}
	
	public static function count($condition = array())
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_News_Models_Mysql_Cron();
		$dao->setConnection($conn);
		return $dao->count($condition);
	}
	
	public function vtc($linkId)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="vtc" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "vtc" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Check time
			 */
			if (($htmlString = @file_get_contents($row['source'])) === false) {
				continue;	
			}
			$html = str_get_html($htmlString);
			$categories = explode('-', $row['category_ids']);
			
			$container = $html->find('.newsList', 0);
			$articles = $container->find('.newsItems');
			
			foreach ($articles as $index => $item) {
				$linkContainer = $item->find('.title', 0);
				$linkSource = $row['website'] . $linkContainer->href;
				$description = $item->find('.desc', 0);
				$description = $description->plaintext;
				$description = trim(str_replace('(VTC News)', '', $description));
				$description = ltrim($description, '-');
				$description = trim($description);
				
				$title = $linkContainer->plaintext;
				$title = trim($title);
				
				$image = $item->find('img', 0);
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign($title, '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => $title,
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => trim($description),
							'created_date' => date('Y-m-d H:i:s'),
							'link_source' => $linkSource,
							'image_url' => (isset($image->src)) ? $image->src : null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'vtc',
							'article_photo' => 0,
							'article_video' => 0,
						);
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	public function vnexpress($linkId = null)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId; 
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="vnexpress" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "vnexpress" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			$row['source'] = strtolower($row['source']); 
			/**
			 * Check time
			 */
			if (($htmlString = @file_get_contents($row['source'])) === false) {
				continue;	
			}
			$html = str_get_html($htmlString);
			$container = $html->find('div.content-center', 0);
			$topArticle = $container->find('div.folder-top', 0);
			$categories = explode('-', $row['category_ids']);
			
			if ($topArticle != null) {
				$linkSource = $topArticle->find('a', 0)->href;
				
				/**
				 * Check exist
				 */
				$link = $row['website'] . $linkSource;
				$link = strtolower($link);
				
				$pTitle = $topArticle->find('p', 0);
				/**
				 * Article Icon 
				 */
				$articlePhoto = 0;
				$articleVideo = 0;
				$articleIcons = $pTitle->find('img');
				if (count($articleIcons) > 0) {
					foreach ($articleIcons as $icon) {
						if (strpos($icon->src, 'video') !== false) {
							$articleVideo = 1;
						}
						if (strpos($icon->src, 'photo') !== false) {
							$articlePhoto = 1;
						}
					}
				}
				
				$title = $topArticle->find('a.link-topnews', 0)->plaintext;
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign($title, '-', true));
				if (null == $existsArticle) {
					$itemTime = $topArticle->find('label.item-time', 0)->plaintext;
					$itemTime = trim($itemTime);
					
					$itemDate = $topArticle->find('label.item-date', 0)->plaintext;
					$itemDate = str_replace('|', '', trim($itemDate));
					$itemDate = str_replace('&nbsp;', '', trim($itemDate));
					$itemDate = str_replace('/', '-', trim($itemDate));
					
					$createdDate = date('Y-m-d H:i:s', strtotime($itemDate . ' ' . $itemTime));
					
					$image = $topArticle->find('img.img-topsubject', 0);
					$description = $topArticle->find('p', 1)->plaintext;
					$article = array(
							'title' => $title,
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => trim($description),
							'created_date' => ($createdDate) ? $createdDate : date('Y-m-d H:i:s'),
							'link_source' => strtolower($link),
							'image_url' => (isset($image->src)) ? $row['website'] . $image->src : null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'vnexpress',
							'article_photo' => $articlePhoto,
							'article_video' => $articleVideo,
						);
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			
			$articles = $container->find('div.folder-news');
			foreach ($articles as $index => $item) {
				if ($index < count($articles) - 2) {
					$linkSource = $item->find('a', 0)->href;
					$link = $row['website'] . $linkSource;
					$title = $item->find('a.link-title', 0)->plaintext;
					
					$pTitle = $item->find('p', 0);
					/**
					 * Article Icon 
					 */
					$articlePhoto = 0;
					$articleVideo = 0;
					$articleIcons = $pTitle->find('img');
					if (count($articleIcons) > 0) {
						foreach ($articleIcons as $icon) {
							if (strpos($icon->src, 'video') !== false) {
								$articleVideo = 1;
							}
							if (strpos($icon->src, 'photo') !== false) {
								$articlePhoto = 1;
							}
						}
					}
					
					/**
					 * Check article exists
					 */
					$existsArticle = $articleDao->getByLink($link);
					if (null == $existsArticle) {
						$itemTime = $item->find('label.item-time', 0)->plaintext;
						$itemTime = trim($itemTime);
						
						$itemDate = $item->find('label.item-date', 0)->plaintext;
						$itemDate = str_replace('|', '', trim($itemDate));
						$itemDate = str_replace('&nbsp;', '', trim($itemDate));
						$itemDate = str_replace('/', '-', trim($itemDate));
						
						$createdDate = date('Y-m-d H:i:s', strtotime($itemDate . ' ' . $itemTime));
					
						$image = $item->find('img.img-subject', 0);
						$description = $item->find('p', 1)->innertext;
						$article = array(
								'title' => trim($title),
								'slug' => Gio_Core_String::removeSign($title, '-', true),
								'description' => trim($description),
								'created_date' => ($createdDate) ? $createdDate : date('Y-m-d H:i:s'),
								'link_source' => strtolower($link),
								'image_url' => (isset($image->src)) ? $row['website'] . $image->src : null,
								'category_ids'	=> $row['category_ids'],
								'website'	=> 'vnexpress',
								'article_photo' => $articlePhoto,
								'article_video' => $articleVideo,
							);
						$addArticles[] = $article;
						$addCategories[] = $categories;
					}
				}
			}
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	public function thethaovanhoa($linkId = null)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="thethaovanhoa" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "thethaovanhoa" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Check time
			 */
			if (($htmlString = @file_get_contents($row['source'])) === false) {
				continue;	
			}
			$html = str_get_html($htmlString);
			$container = $html->find('div.round_left', 0);
			$container = $container->find('div.content', 0);
			$categories = explode('-', $row['category_ids']);

			$articles = $container->find('div.item');
			foreach ($articles as $index => $item) {
				$linkSource = $item->find('a', 0)->href;
				$link = $row['website'] . $linkSource;
				$title = $item->find('div.title', 0)->plaintext;
				
				$image = $item->find('img', 0);
				$description = $item->find('div.sapo', 0)->innertext;
				
				$itemDateTime = $item->find('div.time', 0)->plaintext;
				$itemDateTime = str_replace('/', '-', trim($itemDateTime));
				
				$createdDate = date('Y-m-d H:i:s', strtotime($itemDateTime));

				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign($title, '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => trim($title),
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => trim($description),
							'created_date' => $createdDate,
							'link_source' => $link,
							'image_url' => (isset($image->src)) ? $image->src : null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'thethaovanhoa',
						);
						
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	public function dantri($linkId = null)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="dantri" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "dantri" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Check time
			 */
			if (($htmlString = @file_get_contents($row['source'])) === false) {
				continue;	
			}
			
			$html = str_get_html($htmlString);
			$container = $html->find('div.fl', 0);
			$categories = explode('-', $row['category_ids']);

			$articles = $container->find('div.mt3');
			foreach ($articles as $index => $item) {
				$linkSource = $item->find('a', 0)->href;
				$link = $row['website'] . $linkSource;
				$title = $item->find('a.fon6', 0)->plaintext;
				
				$image = $item->find('img.img130', 0);
				$description = $item->find('div.wid324', 0);
				$description = (isset($description->plaintext)) ? $description->plaintext : null;
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign(trim($title), '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => trim($title),
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => trim($description),
							'created_date' => date('Y-m-d H:i:s'),
							'link_source' => $link,
							'image_url' => (isset($image->src)) ? $image->src : null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'dantri',
						);
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	public function zing($linkId = null)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="zing" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "zing" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Get html from link
			 */
			if (($htmlString = @file_get_contents($row['source'])) === false) {
				continue;	
			}
			
			$html = str_get_html($htmlString);
			/**
			 * Get main container
			 */
			//$container = $html->find('div[id=channelList]', 0);
			$categories = explode('-', $row['category_ids']);

			/**
			 * Get articles by div with css class is cont09
			 */
			$articles = $html->find('div.cont09');
			foreach ($articles as $index => $item) {
				$linkSource = $item->find('a', 0)->href;
				$link = $linkSource;
				/**
				 * Get article title
				 */
				$title = $item->find('a.lnk02', 0)->plaintext;
				
				$imageContainer = $item->find('div.cont09im', 0);
				$image = (isset($imageContainer->innertext)) ? $imageContainer->find('img', 0) : null;
				$description = $item->find('p.cont09txt', 0)->innertext;
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign(trim($title), '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => trim($title),
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => trim($description),
							'created_date' => date('Y-m-d H:i:s'),
							'link_source' => $link,
							'image_url' => (isset($image->src)) ? $image->src : null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'zing',
					);
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	public function megafun($linkId = null)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="megafun" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "megafun" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Get html from link
			 */
			if (($htmlString = @file_get_contents($row['source'])) === false) {
				continue;	
			}
			$html = str_get_html($htmlString);
			/**
			 * Get main container
			 */
			$categories = explode('-', $row['category_ids']);

			/**
			 * Top Article
			 */
			$topContainer = $html->find('.item-list', 0);
			$lis = $topContainer->find('li');
			for ($i = 0; $i < count($lis); $i++) {
				$linkSource = $lis[$i]->find('a.title', 0);
				$title = $linkSource->plaintext;
				$link = $row['website'] . $linkSource->href;
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign(trim($title), '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => trim($title),
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => null,
							'created_date' => date('Y-m-d H:i:s'),
							'link_source' => $link,
							'image_url' => null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'megafun',
					);
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			/**
			 * carol1
			 */
			$carol1Container = $html->find('.item-list', 1);
			$lis = $carol1Container->find('li');
			for ($i = 0; $i < count($lis); $i++) {
				$linkSource = $lis[$i]->find('a.title', 0);
				$title = $linkSource->plaintext;
				$link = $row['website'] . $linkSource->href;
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getByLink($link);
				if (null == $existsArticle) {
					$article = array(
							'title' => trim($title),
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => null,
							'created_date' => date('Y-m-d H:i:s'),
							'link_source' => $link,
							'image_url' => null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'megafun',
					);
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	public function kenh14($linkId = null)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="kenh14" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "kenh14" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Get html from link
			 */
			if (($htmlString = @file_get_contents($row['source'])) === false) {
				continue;	
			}
			
			$html = str_get_html($htmlString);
			/**
			 * Get main container
			 */
			$categories = explode('-', $row['category_ids']);
			
			$container = $html->find('div.maincontent', 0);
			
			/**
			 * Top article
			 */
			$topContainer = $container->find('div.mainfeature', 0);
			if (isset($topContainer->innertext) && $topContainer->innertext != null) {
				$linkSource = $topContainer->find('a', 0)->href;
				$link = $row['website'] . $linkSource;
				$title = $topContainer->find('.title', 0)->plaintext;
				$imageContainer = $topContainer->find('div.imgwrapper', 0);
				
				$image = (isset($imageContainer->innertext)) ? $imageContainer->find('img', 0) : null;
				$description = $topContainer->find('div.sapo', 0)->plaintext;
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign(trim($title), '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => trim($title),
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => trim(strip_tags($description)),
							'created_date' => date('Y-m-d H:i:s'),
							'link_source' => $link,
							'image_url' => (isset($image->src)) ? $image->src : null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'kenh14',
					);
					
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			$articlesContainer = $container->find('div.listnews', 0);
			$articles = $articlesContainer->find('div.item');
			foreach ($articles as $index => $item) {
				$linkSource = $item->find('a', 0)->href;
				$link = $row['website'] . $linkSource;
				
				$date = $item->find('div.meta', 0);
				$date = str_replace('/', '-', trim($date->plaintext));
				$date = date('Y-m-d H:i:s', strtotime($date));
				
				/**
				 * Get article title
				 */
				$titleContainer = $item->find('div.title', 0);
				$title = $titleContainer->plaintext;
				
				$image = $item->find('img', 0);
				
				$description = $item->find('div.sapo', 0)->plaintext;
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getByLink($link);
				if (null == $existsArticle) {
					$article = array(
							'title' => trim($title),
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => trim(strip_tags($description)),
							'created_date' => $date,
							'link_source' => $link,
							'image_url' => (isset($image->src)) ? $image->src : null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'kenh14',
					);
					
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	public function ngoisao($linkId = null)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="ngoisao" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "ngoisao" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Check time
			 */
			$rssData = simplexml_load_file($row['source']);
			$articles = $rssData->channel->item;
			$categories = explode('-', $row['category_ids']);
			
			foreach ($articles as $index => $item) {
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign(trim($item->title), '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => trim($item->title),
							'slug' => Gio_Core_String::removeSign($item->title, '-', true),
							'description' => trim(strip_tags($item->description)),
							'created_date' => date('Y-m-d H:i:s', strtotime($item->pubDate)),
							'link_source' => (string)$item->link,
							'image_url' => null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'ngoisao',
							'article_photo' => 0,
							'article_video' => 0,
						);
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	public function haitugio($linkId = null)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="haitugio" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "haitugio" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Check time
			 */
			$rssData = simplexml_load_file($row['source']);
			$articles = $rssData->channel->item;
			$categories = explode('-', $row['category_ids']);
			
			foreach ($articles as $index => $item) {
				$title = trim(Gio_Core_String::stripCdata($item->title));
				$description = Gio_Core_String::stripCdata($item->description);
				$date = $item->pubDate;
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign(trim($title), '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => $title,
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => trim(strip_tags($description)),
							'created_date' => date('Y-m-d H:i:s', strtotime($date)),
							'link_source' => trim((string)$item->link),
							'image_url' => null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'haitugio',
							'article_photo' => 0,
							'article_video' => 0,
						);
						
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	public function tuoitre($linkId = null)
	{
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="tuoitre" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "tuoitre" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Check time
			 */
			$rssData = simplexml_load_file($row['source']);
			$articles = $rssData->channel->item;
			$categories = explode('-', $row['category_ids']);
			
			foreach ($articles as $index => $item) {
				$description = Gio_Core_String::stripCdata($item->description);
				$date = str_replace('/', '-', $item->pubdate);
				$date = substr($date, 0, -3);
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign(trim($item->title), '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => trim($item->title),
							'slug' => Gio_Core_String::removeSign($item->title, '-', true),
							'description' => trim(strip_tags($description)),
							'created_date' => date('Y-m-d H:i:s', strtotime($date)),
							'link_source' => (string)$item->link,
							'image_url' => null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'tuoitre',
							'article_photo' => 0,
							'article_video' => 0,
						);
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	public function thanhnien($linkId = null)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="thanhnien" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "thanhnien" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Get html from link
			 */
			if (($htmlString = @file_get_contents($row['source'])) === false) {
				continue;	
			}
			$html = str_get_html($htmlString);
			/**
			 * Get main container
			 */
			$categories = explode('-', $row['category_ids']);
			
			$container = $html->find('div.newstype', 0);
			$articles = $container->find('div.cm-content');
			
			foreach ($articles as $index => $item) {
				$linkSource = $item->find('a', 0)->href;
				$link = $linkSource;
				
				/**
				 * Get article title
				 */
				$titleContainer = $item->find('a', 0);
				$title = $titleContainer->plaintext;
				
				$image = $item->find('img', 0);
				
				$description = $item->find('div.cm-des', 0)->innertext;
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign(trim($title), '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => trim($title),
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => trim(strip_tags($description)),
							'created_date' => date('Y-m-d H:i:s'),
							'link_source' => $link,
							'image_url' => (isset($image->src)) ? $row['website'] . $image->src : null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'thanhnien',
					);
					
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			
			$lastArticle = $container->find('div.cm-content-last', 0);
			if ($lastArticle && isset($lastArticle->innertext) && $lastArticle->innertext != null) {
				$linkSource = $lastArticle->find('a', 0)->href;
				$link = $linkSource;
				
				/**
				 * Get article title
				 */
				$titleContainer = $lastArticle->find('a', 0);
				$title = $titleContainer->plaintext;
				
				$image = $lastArticle->find('img', 0);
				
				$description = $lastArticle->find('div.cm-des', 0)->innertext;
				
				$existsArticle = $articleDao->getByLink($link);
				if (null == $existsArticle) {
					$article = array(
							'title' => trim($title),
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => trim(strip_tags($description)),
							'created_date' => date('Y-m-d H:i:s'),
							'link_source' => $link,
							'image_url' => (isset($image->src)) ? $row['website'] . $image->src : null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'thanhnien',
					);
					
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	public function nguoilaodong($linkId = null)
	{
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="nguoilaodong" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "nguoilaodong" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Check time
			 */
			$rssData = simplexml_load_file($row['source']);
			$articles = $rssData->channel->item;
			$categories = explode('-', $row['category_ids']);
			
			foreach ($articles as $index => $item) {
				$title = trim(Gio_Core_String::stripCdata($item->title));
				$description = Gio_Core_String::stripCdata($item->description);
				$date = $item->pubDate;
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign(trim($title), '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => $title,
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => trim(strip_tags($description)),
							'created_date' => date('Y-m-d H:i:s', strtotime($date)),
							'link_source' => trim((string)$item->link),
							'image_url' => null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'nguoilaodong',
							'article_photo' => 0,
							'article_video' => 0,
						);
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	function haisao($linkId)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="haisao" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "haisao" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Check time
			 */
			$rssData = simplexml_load_file($row['source']);
			$articles = $rssData->channel->item;
			$categories = explode('-', $row['category_ids']);
			
			foreach ($articles as $index => $item) {
				$title = trim($item->title);
				$title = Gio_Core_String::stripCdata($title);
				$description = Gio_Core_String::stripCdata($item->description);
				$description = strip_tags($description);
				$link = Gio_Core_String::stripCdata($item->link);
				$link = trim($link);
				$date = Gio_Core_String::stripCdata($item->pubDate);
				$date = trim($date);
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign(trim($title), '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => $title,
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => $description,
							'created_date' => date('Y-m-d H:i:s', strtotime($date)),
							'link_source' => $link,
							'image_url' => null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'haisao',
							'article_photo' => 0,
							'article_video' => 0,
						);
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	public function autopro($linkId)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="autopro" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "autopro" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		$request = new Gio_Core_Request();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			$pageIndex = $request->getParam('page_index');
			$pageIndex = ($pageIndex > 0) ? '/trang-' . $pageIndex . '.chn' : null;
			if ($pageIndex) {
				$row['source'] = str_replace('.chn', $pageIndex, $row['source']);
			}
			
			/**
			 * Check time
			 */
			if (($htmlString = @file_get_contents($row['source'])) === false) {
				continue;	
			}
			
			$html = str_get_html($htmlString);
			$container = $html->find('div.divCMTC', 0);
			$categories = explode('-', $row['category_ids']);

			$articles = $container->find('div[id=smallava2]');
			foreach ($articles as $index => $item) {
				$linkSource = $item->find('a', 0)->href;
				$link = $row['website'] . $linkSource;
			
				$title = $item->find('h1', 0)->plaintext;
				
				$image = $item->find('img', 0);
				$description = $item->find('h2', 0)->plaintext;
				$description = str_replace('Xem tiếp', '', $description);
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign(trim($title), '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => trim($title),
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => trim($description),
							'created_date' => date('Y-m-d H:i:s'),
							'link_source' => $link,
							'image_url' => (isset($image->src)) ? $image->src : null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'autopro',
						);
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
	
	public function bongda($linkId)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="bongda" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "bongda" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Check time
			 */
			if (($htmlString = @file_get_contents($row['source'])) === false) {
				continue;	
			}
		
			$html = str_get_html($htmlString);
			$container = $html->find('div[id=ctl00_BD_cat_pnlListView]', 0);
			$categories = explode('-', $row['category_ids']);

			$articles = $container->find('.read_more');
			$dates = $container->find('.date');
			
			foreach ($articles as $index => $item) {
				$linkSource = $item->href;
				$link = $row['website'] . $linkSource;
				$title = $item->plaintext;
				$date = $dates[$index]->innertext;
				$date = str_replace('/', '-', $date);
				$date = date('Y-m-d H:i:s', strtotime($date));
				$date = ($date != '1970-01-01 07:00:00') ? $date : date('Y-m-d H:i:s');
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign(trim($title), '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => trim($title),
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => null,
							'created_date' => $date,
							'link_source' => $link,
							'image_url' => null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'bongda',
						);
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	} 
	
	public function tintuconline($linkId)
	{
		ini_set("memory_limit", "256M");
		$conn = Gio_Db_Connection::getConnection();
		if ($linkId) {
			$ext = ($linkId == -1) ? null :  ' AND cron_id = ' . (int)$linkId;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron WHERE website_name="tintuconline" 
						AND is_locked = 0 AND status = "active"' . $ext;
		} else {
			$numCategories = isset($this->_configs['auto_update_num_category'])
								? $this->_configs['auto_update_num_category'] : 5;
			$sql = 'SELECT * FROM ' . $conn->_tablePrefix . 'news_cron
					WHERE status = "active" AND TIMESTAMPDIFF(SECOND, last_update, "' . date('Y-m-d H:i:s') . '") > frequency
						AND website_name = "tintuconline" AND is_locked = 0 
					ORDER BY priority
					LIMIT ' . (int)$numCategories;
		}
		$rsSql = $conn->query($sql);
		
		$addArticles = array();
		$addCategories = array();
		
		$articleDao = new Modules_News_Models_Mysql_Article();
		$articleDao->setConnection($conn);
		
		while($row = $conn->fetchAll($rsSql)) {
			/**
			 * Check time
			 */
			$rssData = simplexml_load_file($row['source']);
			$articles = $rssData->channel->item;
			$categories = explode('-', $row['category_ids']);
			
			foreach ($articles as $index => $item) {
				$title = trim($item->title);
				$title = Gio_Core_String::stripCdata($title);
				$description = Gio_Core_String::stripCdata($item->description);
				$description = strip_tags($description);
				$link = Gio_Core_String::stripCdata($item->link);
				$link = trim($link);
				$date = Gio_Core_String::stripCdata($item->pubDate);
				$date = trim($date);
				
				/**
				 * Check article exists
				 */
				$existsArticle = $articleDao->getBySlug(Gio_Core_String::removeSign(trim($title), '-', true));
				if (null == $existsArticle) {
					$article = array(
							'title' => $title,
							'slug' => Gio_Core_String::removeSign($title, '-', true),
							'description' => $description,
							'created_date' => date('Y-m-d H:i:s', strtotime($date)),
							'link_source' => $link,
							'image_url' => null,
							'category_ids'	=> $row['category_ids'],
							'website'	=> 'tintuconline',
							'article_photo' => 0,
							'article_video' => 0,
						);
					$addArticles[] = $article;
					$addCategories[] = $categories;
				}
			}
			
			/**
			 * Update time for cron
			 */
			$sql = 'UPDATE ' . $conn->_tablePrefix . 'news_cron SET last_update = "' . date('Y-m-d H:i:s') . '"
						WHERE cron_id = ' . $conn->escape($row['cron_id']);
			$rsUpdate = $conn->query($sql);
			$conn->freeResult($rsUpdate);
		}
		
		$conn->freeResult($rsSql);
		/**
		 * Add articles, Add Categories
		 */
		if ($addArticles && $addCategories) {
			$this->addArticles[] = $addArticles;
			$this->addCategories[] = $addCategories;
		}
	}
}